var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    categoryList: [],
    currentCategory: {},
    currentSubCategoryList: {},
    scrollLeft: 0,
    scrollTop: 0,
    goodsCount: 0,
    scrollHeight: 0,
    goodsList:[],
    page: 1,
    size: 100,
    StorageView: api.StorageView
  },
  onShareAppMessage: function () {
    var userInfo = wx.getStorageSync('userInfo');
    return {
      title: '您身边的智能机器人一站式购物平台！',
      desc: 'Spring 机器人小商场',
      path: '/pages/index/index?userId=' + userInfo.userId
    }
  },
  onLoad: function (options) {
    this.getCatalog();
    // 页面初始化 options为页面跳转所带来的参数
    var userId = options.userId;
    if (userId != null && userId != "") {
      wx.setStorageSync('parent_user_id', userId);
      //检查是否登录
      if (app.globalData.hasLogin) {
        //已登陆  就检测更新推荐人信息
        util.request(api.memberInfo, { pUserId: userId });

      } else {
        wx.navigateTo({ url: "/pages/auth/login/login" });
      }
    }
  },
  getCatalog: function () {
    //CatalogList
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });
    util.request(api.CatalogList).then(function (res) {
        that.setData({
          categoryList: res.data.categoryList,
          currentCategory: res.data.currentCategory,
          goodsList: res.data.goodsList
        });
        wx.hideLoading();
      });
    util.request(api.GoodsCount).then(function (res) {
      that.setData({
        goodsCount: res.data.goodsCount
      });
    });

  },
  getCurrentCategory: function (id) {
    let that = this;
    util.request(api.CatalogCurrent, { id: id, page: that.data.page, size: that.data.size })
      .then(function (res) {
        console.log(res.data.currentCategory);
        that.setData({
          currentCategory: res.data.currentCategory,
          goodsList: res.data.goodsList
        });
      });
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  switchCate: function (event) {
    var that = this;
    var currentTarget = event.currentTarget;
    if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
      return false;
    }
    this.getCurrentCategory(event.currentTarget.dataset.id);
  },
   onPullDownRefresh: function () {
     this.getCatalog();
    this.getCurrentCategory(this.data.currentCategory.id);
    wx.stopPullDownRefresh();
  }
})